package com.cskaoyan.service.admin;

import com.cskaoyan.bean.MarketComment;
import com.cskaoyan.bean.MarketCommentExample;
import com.cskaoyan.mapper.MarketCommentMapper;
import com.cskaoyan.model.admin.bo.BaseParam;
import com.cskaoyan.model.admin.bo.CommentBO;
import com.cskaoyan.model.admin.vo.CommentListOfCommentRespVO;
import com.cskaoyan.model.admin.vo.CommentRespVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName: CommentServiceImpl
 * @Author: WangEnzhao
 * @Description: 商品评论模块
 * @CreateTime: 2021/12/19:40:00
 */
@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    MarketCommentMapper marketCommentMapper;

    @Override
    public CommentRespVO selectComment(CommentBO commentBO, BaseParam baseParam) {
        if(commentBO == null){
            commentBO = new CommentBO();
        }

        //返回的数据
        CommentRespVO commentRespVO = new CommentRespVO();

        //查询total数据
        MarketCommentExample marketCommentExample = new MarketCommentExample();
        MarketCommentExample.Criteria criteria = marketCommentExample.createCriteria();

        try{
            if(commentBO.getUserId() != null && !commentBO.getUserId().equals("")){
                criteria.andUserIdEqualTo(Integer.parseInt(commentBO.getUserId()));
            }
            if(commentBO.getValueId() != null && !commentBO.getValueId().equals("")){
                criteria.andValueIdEqualTo(Integer.parseInt(commentBO.getValueId()));
            }
            criteria.andDeletedEqualTo(false);
        }catch (Exception e){
            e.printStackTrace();
            return commentRespVO;
        }

        long total = marketCommentMapper.countByExample(marketCommentExample);
        commentRespVO.setLimit(baseParam.getLimit());
        commentRespVO.setTotal(new Integer((int)total));
        commentRespVO.setPages(commentRespVO.getTotal()/commentRespVO.getLimit() + 1);
        commentRespVO.setPage(baseParam.getPage());

        //查询list
        List<CommentListOfCommentRespVO> commentListOfCommentRespVOS = marketCommentMapper.selectComment(commentBO, baseParam);
        commentRespVO.setList(commentListOfCommentRespVOS);


        return commentRespVO;
    }

    @Override
    public void deleteComment(String id) {
        MarketCommentExample marketCommentExample = new MarketCommentExample();
        MarketComment marketComment = new MarketComment();
        marketComment.setDeleted(true);
        marketCommentExample.createCriteria().andIdEqualTo(Integer.parseInt(id));
        marketCommentMapper.updateByExampleSelective(marketComment,marketCommentExample);
    }



}
